package com.ck.common.utils.security.sign;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.Objects;

public class CtMD5 {

    public static String getFileMD5(File file) {
        String md5 = null;
        if (!file.isFile()) {
            return null;
        }
        //messagedigest一定要为局部变量，为了保证线程安全。写成全局变量在多线程情况下，md5值计算将出现问题。
        MessageDigest digest;
        FileInputStream in = null;
        byte buffer[] = new byte[8192];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer)) != -1) {
                digest.update(buffer, 0, len);
            }
            md5 = toHexString(digest.digest(), "");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return md5;
    }

    private static String toHexString(byte[] bytes, String separator) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xFF & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex).append(separator);
        }
        return hexString.toString();
    }

    public static String MD5_32(String s) {
        return MD5(s);
    }

    public static String MD5_16(String s) {
        return Objects.requireNonNull(MD5(s)).substring(8, 24);
    }

    public static String MD5(String s) {
        char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'A', 'B', 'C', 'D', 'E', 'F'};
        try {
            byte[] btInput = s.getBytes();
            // 获得MD5摘要算法的 MessageDigest 对象
            MessageDigest mdInst = MessageDigest.getInstance("MD5");
            // 使用指定的字节更新摘要
            mdInst.update(btInput);
            // 获得密文
            byte[] md = mdInst.digest();
            // 把密文转换成十六进制的字符串形式
            int j = md.length;
            char[] str = new char[j * 2];
            int k = 0;
            for (byte byte0 : md) {
                str[k++] = hexDigits[byte0 >>> 4 & 0xf];
                str[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(str);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void main(String[] args) throws IOException {
        String path = "C:\\Users\\Yangey\\IdeaProjects\\G3Test6\\ideaIU-2018.1.4.dmg";
        long time3 = System.currentTimeMillis();
        String md52 = getFileMD5(new File(path));
        long time4 = System.currentTimeMillis();
        System.out.println("MD5值3" + "    耗时：" + (time4 - time3));
        System.out.println("MD5值3" + md52);

        //ad95724d7c90cd04ea48dcb9e5f3bbc6

        //System.out.println("MD5:"+DigestUtils.md5Hex("WANGQIUYUN"));
    }
}
